import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.net.*;
import java.util.Enumeration;

/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2021-12-25
 * Time:15:28
 */
public class User extends JFrame implements ActionListener, Runnable{

    public static void main(String[] args) {

        Font font = new Font("楷体",0,26);//创建字体对象
        User.InitGlobalFont(font);//将默认字体设置为全局字体
        try {
            User user1 = new User("用户1", InetAddress.getByName("localhost"),2345,2453);
            User user2 = new User("用户2", InetAddress.getByName("localhost"),2453,2345);
            user1.setBounds(100,100,600,500);
            user2.setBounds(800,100,600,500);

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        BigDecimal bigDecimal = new BigDecimal(50);

    }

    JTextField outMessage = new JTextField(12);//用于输入发送的数据
    JTextArea inMessage = new JTextArea(12,20);//用于展示接收和发送的消息
    JButton b = new JButton("发送数据");//发送按钮
    InetAddress dstAddr;//目标地址
    int dst;//目标端口号
    int src;//当前客户端的端口号

    public User(String userName, InetAddress dstAddr,int dst,int src)//构造方法 - 创建客户端窗口
    {
        super(userName);//定义客户端窗口标题
        this.dstAddr = dstAddr;
        this.dst = dst;
        this.src = src;
        b.addActionListener(this);

        setSize(600,500);
        setVisible(true);

        JPanel p = new JPanel();
        p.add(outMessage);//发送的数据
        p.add(b);//添加发送按钮

        Container con = getContentPane();
        con.add(new JScrollPane(inMessage), BorderLayout.CENTER);//添加消息框
        con.add(p, BorderLayout.NORTH);//添加发送消息的面板

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        validate();

        Thread thread = new Thread(this);
        thread.start(); //线程负责接收数据

    }



    @Override
    public void actionPerformed(ActionEvent e)
    {
        // 单击按钮发送数据
        byte b[] = outMessage.getText().trim().getBytes();
        inMessage.append("向对方发出了信息："+"\n");
        inMessage.append(outMessage.getText()+"\n");
        outMessage.setText("");
        Server server = new Server(dstAddr,b,dst);
        Thread thread = new Thread(server);
        thread.start();

    }

    @Override
    public void run()
    {
        // 接收数据
        DatagramPacket pack = null;
        DatagramSocket mail = null;
        byte b[] = new byte[8192];
        try
        {
            pack = new DatagramPacket(b,b.length);
            mail = new DatagramSocket(src);
        }
        catch(Exception e){}

        while(true)
        {
            try
            {
                mail.receive(pack);
                String message = new String(pack.getData(),0,pack.getLength());
                inMessage.append("收到来自：" + pack.getAddress()+"的信息：\n");
                inMessage.append(message+"\n");
                inMessage.setCaretPosition(inMessage.getText().length());
            }
            catch(Exception e){}
        }

    }

    //修改swing全局字体 - 在main方法中生成其他GUI图形界面之前调用，可以将字体设置为我们传参的字体
    public static void InitGlobalFont(Font f){
        FontUIResource fontRes = new FontUIResource(f);
        for (Enumeration<Object> keys = UIManager.getDefaults().keys(); keys.hasMoreElements();) {
            Object key = keys.nextElement();
            Object value = UIManager.get(key);
            if (value instanceof FontUIResource) {
                UIManager.put(key, fontRes);
            }
        }
    }

}
